(function () {
    init();

    function init() {
        getConfig();
        window.onresize = resizeWin
        setTimeout(resizeWin, 1000)
    }

    function windowHeight() {
        var de = document.documentElement;
        return self.innerHeight || (de && de.clientHeight) || document.body.clientHeight;
    }

    function getVueData(data) {
        return {
            root: data,
            leftMenuData: [],
            tempData: [],
            currentKeyPath: "",
            iframe_height: "800px",
            marginLeft: "400px",
            activeIndex: '1',
            editableTabsValue2: '1',
            editableTabs2: [{
                title: '首页',
                name: '1',
                url: "welcome.html"
            }],
            tabIndex: 1
        }
    }

    function getConfig() {
        var marginLeftVar = "400px";
        var Main;

        var getVueMethods = function () {
            var methods = {
                handleOpen: function (key, keyPath) {

                    vue.$data.currentKeyPath = keyPath;
                    console.log("-------------->handleOpen<----------------",vue.$data.currentKeyPath)
                    menuItemClick(key, keyPath);
                },
                handleClose: function (key, keyPath) {
                    console.log(key, keyPath);
                },
                handleSelect: function (key, keyPath) {
                    getMenuData(function (response) {
                        window.vue.$data.leftMenuData = response.data.data
                    }, key)
                },
                addTab: function (menu) {
                    var tabList = window.vue.editableTabs2;
                    var isExist = false;
                    for (var i = 0; i < tabList.length && tabList.length >= 1; i++) {
                        //if (tabList[i].name.indexOf(menu.id) >= 0) {
                        if (tabList[i].name===menu.id) {
                            isExist = true;
                            this.editableTabsValue2 = menu.id;
                            break;
                        }
                    }
                    if (isExist) {

                    } else {
                        let newTabName = ++this.tabIndex + '';
                        this.editableTabs2.push({
                            title: menu.name || 'New Tab',
                            name: menu.id,
                            url: menu.url
                        });
                        this.editableTabsValue2 = menu.id;
                    }
                },
                searchMenu: searchMenu,
                handleTabsEdit: function (targetName, action) {
                    if (action === 'add') {
                        let newTabName = ++this.tabIndex + '';
                        this.editableTabs2.push({
                            title: 'New Tab',
                            name: newTabName,
                            content: 'New Tab content'
                        });
                        this.editableTabsValue2 = newTabName;
                    }
                    if (action === 'remove') {
                        let tabs = this.editableTabs2;
                        let activeName = this.editableTabsValue2;
                        if (activeName === targetName) {
                            tabs.forEach(function (tab, index) {
                                if (tab.name === targetName) {
                                    let nextTab = tabs[index + 1] || tabs[index - 1];
                                    if (nextTab) {
                                        activeName = nextTab.id;
                                    }
                                }
                            });
                        }
                        this.editableTabsValue2 = activeName;
                        this.editableTabs2 = tabs.filter(tab => tab.name !== targetName)
                    }
                }
            }
            return methods;
        }
        getMenuData(function (axios) {
            Main = {
                el: "#app",
                data: getVueData(axios.data.data),
                methods: getVueMethods(),
                watch: getWatchers()
            }
            window.vue = new Vue(Main);
            setTimeout(function () {
                document.querySelector("#topbar li").click()
            }, 300)

        });
    }

    //监控动态计算的数据
    function getWatchers() {
        var result = {
            tempData: mergeMenu
        }
        return result;
    }

    function mergeMenu() {
        var target;
        if (this.leftMenuData.length === 0) {
            //初始化时，直接将缓存的数据复制给左边的菜单
            this.leftMenuData = this.tempData
        } else {
            if (this.tempData.length === 0) {
            } else {
                var firstTemp = this.tempData[0];
                //左边菜单有数据时，遍历左边菜单，找到等于当前返回值的parentId的数据节点，将temp赋给children
                var keyArray=vue.$data.currentKeyPath;
                if (keyArray.length===1){
                    target = this.leftMenuData.filter(function (x) {
                        return x.id == keyArray[0]
                    })
                }else{
                    target = this.leftMenuData.filter(function (x) {
                        return x.id == keyArray[0]
                    })[0].children.filter(function (second) {
                        return second.id==keyArray[1]
                    })
                }
                Vue.set(target[0], "children", this.tempData)
            }

        }

    }

    function resizeWin() {
        $("#contentWrap").css("height", $(window).height() - $("#topbar").height() - 8);
        $("#contentWrap").css("width", $(window).width() - $("#leftMenu").width() - 5);
        var iframe_height_var = windowHeight() - $("#topbar").height() - $(".el-tabs__header").height() - 25
        vue.$data.iframe_height = iframe_height_var + "px"
    }

    function getMenuData(callback, parentIdVar) {
        if (typeof (parentIdVar) == "undefined") {
            parentIdVar = 0;
        }
        axios.get(`${_config_.server}${_config_.app.menu}?category=menu&parentId=${parentIdVar}`)
            .then(function (response) {
                console.log("axios执行ajax的返回结果为：", response);
                callback(response)
            })
            .catch(function (error) {
                console.log(error);
            });
    }

    function searchMenu() {
        getMenuData(function (axios) {
            vue.$data = getVueData(axios.data.data)
        });
    }

    function menuItemClick(key, keyPath) {
        console.log(`menuItemClick ： key :${key}, keyPath : ${keyPath}`);
        getMenuData(function (response) {
            window.vue.$data.tempData = response.data.data
            console.log("window.vue.$data.tempData:", window.vue.$data.tempData);
        }, key)
    }
})()


